package com.ggh.jinjilive.ui.home.video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemLiveListBinding;
import com.ggh.jinjilive.databinding.ItemVideoGuanliBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.home.video.VideoList;
import com.ggh.jinjilive.mobile.mine.video.PersonalVideoList;
import com.ggh.jinjilive.remotedata.BaseApiModule;

import java.util.List;

public class VideoGuanLiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //管理
    private String management;
    public void management(String management){
        this.management = management;
        notifyDataSetChanged();
    }

    private List<PersonalVideoList> personalVideoListList;

    public void setAdd(List<PersonalVideoList> personalVideoListList) {
        this.personalVideoListList = personalVideoListList;
        notifyDataSetChanged();
    }

    public void Add(List<PersonalVideoList> personalVideoListList) {
        if(null != this.personalVideoListList){
            this.personalVideoListList.addAll(personalVideoListList);
        }else {
            this.personalVideoListList = personalVideoListList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemVideoGuanliBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_video_guanli, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
        ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
        PersonalVideoList personalVideoList = personalVideoListList.get(position);
        if(null != personalVideoList){
            //图片
            Glide.with(holder.binding.RoundedImageView).load(personalVideoList.getCover()).into(holder.binding.RoundedImageView);
            //查看
            holder.binding.numVideoItem.setText(personalVideoList.getLook_num()+"");

            if(management.equals("完成")){
                holder.binding.cbCheck.setVisibility(View.VISIBLE);
                holder.binding.cbCheck.setChecked(personalVideoList.isCheck());
                holder.binding.cbCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(null != addItemOnClickListener){
                            if(!personalVideoList.isCheck()){
                                personalVideoList.setCheck(true);
                                addItemOnClickListener.onSkipClicked(9999997,personalVideoList);
                            }else {
                                personalVideoList.setCheck(false);
                                addItemOnClickListener.onSkipClicked(9999996,personalVideoList);
                            }
                        }
                    }
                });

                holder.binding.icPrivate.setVisibility(View.VISIBLE);
                holder.binding.icComments.setVisibility(View.VISIBLE);
                //私密
                if(personalVideoList.getIs_private() == 1){
                    holder.binding.icPrivate.setImageResource(R.mipmap.icon_set_private);
                }else {
                    holder.binding.icPrivate.setImageResource(R.mipmap.icon_private);
                }
                holder.binding.icPrivate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            if(personalVideoList.getIs_private() == 1){
                                personalVideoList.setIs_private(0);
                            }else {
                                personalVideoList.setIs_private(1);
                            }
                            addItemOnClickListener.onSkipClicked(9999999,personalVideoList);
                        }
                    }
                });
                //设置视频评论区是否可见
                if(personalVideoList.getIs_comment() == 1){
                    holder.binding.icComments.setImageResource(R.mipmap.icon_ban_comments);
                }else {
                    holder.binding.icComments.setImageResource(R.mipmap.icon_comments);
                }
                holder.binding.icComments.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            if(personalVideoList.getIs_comment() == 1){
                                personalVideoList.setIs_comment(0);
                            }else {
                                personalVideoList.setIs_comment(1);
                            }
                            addItemOnClickListener.onSkipClicked(9999998,personalVideoList);
                        }
                    }
                });
            }else {
                holder.binding.cbCheck.setVisibility(View.GONE);
                holder.binding.icPrivate.setVisibility(View.INVISIBLE);
                holder.binding.icComments.setVisibility(View.INVISIBLE);
            }

        }
    }

    @Override
    public int getItemCount() {
        return null == personalVideoListList ? 0 : personalVideoListList.size();
    }



    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemVideoGuanliBinding binding;

        public ItemViewHolder(@NonNull ItemVideoGuanliBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params =  binding.flVideo.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth() / 2 - SizeUtils.dp2px(15);
            binding.flVideo.setLayoutParams(params);
        }
    }

}